package com.yoomiito.app.ui.anyview.goodsinfo.bottom

import android.Manifest
import android.view.LayoutInflater
import android.widget.LinearLayout
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.umeng.socialize.bean.SHARE_MEDIA
import com.yoomiito.app.R
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.isNull
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.model.bean.ShareUrl
import com.yoomiito.app.model.bean.UmShareInfo
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast
import com.yoomiito.app.strFilter
import com.yoomiito.app.ui.anyview.shareview.ShareView
import com.yoomiito.app.utils.AllUtils
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.Type
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.utils.wx.UMengShareUtils
import com.yoomiito.app.widget.dialog.ShareBottomDialog_1
import com.yoomiito.app.widget.dialog.ShopCarDialog

abstract class BaseBottomBar(val context: BaseActivity<*>):LinearLayout(context) {

    public var shopCarDialog: ShopCarDialog? = null

    lateinit var onClick : OnClick

    init {
        LayoutInflater.from(context).inflate(getLayoutId(), getParentView(), true)
    }
    var goodsDetail:GoodsDetail?=null
    private lateinit var shareView:ShareView
    var onClickListener:(ClickEvent)->Unit={}

    abstract fun getLayoutId():Int

    abstract fun getParentView():LinearLayout

    abstract fun initView(goodsDetail : GoodsDetail)

    fun updateGoodsInfo(goodsDetail : GoodsDetail){
        this.goodsDetail = goodsDetail
        initView(goodsDetail)
    }
    fun showShareDialog(){
        val shareDialog = ShareBottomDialog_1(context)
        shareDialog.setOnShareItemClickListener { i->
//            postShareNum()
            when(i){
                1->{
//                    val info = UmShareInfo()
//                    info.shareUrl = goodsDetail?.share_url
//                    info.title = goodsDetail?.title
//                    info.imgUrl = if(goodsDetail?.share_img?.isEmpty()!!)goodsDetail?.pict_url else goodsDetail?.share_img
//                    info.path = "/pages/index/mpcode?scene=${UserInfoConfig.getInviteCode()}_${goodsDetail?.num_iid}"
//                    info.id = "gh_fdaef843ac93"
//                    UMengShareUtils.shareWXMiniProgram(context,info)
                    UMengShareUtils.shareWX(context, goodsDetail?.recommended_language, goodsDetail?.share_url, goodsDetail?.title, goodsDetail?.share_img)
                }//微信好友
                2->{
                    UMengShareUtils.shareWXCircle(context, goodsDetail?.recommended_language, goodsDetail?.share_url, goodsDetail?.title, goodsDetail?.share_img)
                }
                3->share(i)//朋友圈
            }
            shareDialog.dismiss()
        }
        shareDialog.show()
        shareDialog.showSavePic(false)

    }

    private fun postShareNum(){
        YmApi.getYmService()
                .postShareNum(goodsDetail?.num_iid.toString())
                .compose(context.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<BaseModel<*>>(){
                    override fun onSuccess(t: BaseModel<*>?) {
                    }

                    override fun onFail(error: ApiException?) {
                    }
                })
    }

    fun share(type:Int){
        AllUtils.requestPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe { aBoolean ->
                    shareView = ShareView(context,goodsDetail!!)
                    shareView.onPicMakeListener = {
                        when(type){
                            2->{
                                UMengShareUtils.shareWXPic(SHARE_MEDIA.WEIXIN_CIRCLE,context,it)
                            }//朋友圈
                            3->{
                                ImageLoader.getInstance().downloadPic(context,it)
                                "保存成功".showToast()
                            }//保存图片
                        }
                        context.dismissLoading()
                    }
                    getQRCodeUrl()
                }

    }
    private fun getQRCodeUrl(){
        context.showLoading()
        YmApi.getYmService()
                .getQRCodePic(goodsDetail?.num_iid.toString())
                .compose(context.bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<ShareUrl>(){
                    override fun onSuccess(t: ShareUrl?) {
                        t?.url?.strFilter { !it.isNullOrEmpty() }?.let {
                            if (shareView.isNull()){
                                context.dismissLoading()
                            }else{
                                shareView.setQRCodeUrl(it)
                            }
                        }
                    }

                    override fun onFail(error: ApiException?) {
                        context.dismissLoading()
                        "图片生成失败，请稍候再试".showToast()
                    }
                })
    }

    /**
     * 底部栏点击事件
     * @param eventType 事件类型
     * @param goodsType 商品类型
     */
    data class ClickEvent(var eventType:String,var goodsType:Int)


    interface  OnClick{
        fun onClick(goodsDetail: GoodsDetail, type: Int)
    }

    fun setOnClickClick(onClick : OnClick){
        this.onClick = onClick
    }
}